﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using System.Xml;
using System.Resources;
using System.IO;
using System.Reflection;
using Tesla.Service.DataContracts;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace Tesla.Util.Utilities
{
    public static class ResolveMapper
    {
        public static string ResolveMethod(int id)
        {
            ICacheManager cacheManager;
            string contextName = string.Empty;
            XDocument xDoc = null;

            try
            {

                cacheManager = CacheFactory.GetCacheManager();
                if (!cacheManager.Contains("MAPPERXML"))
                {
                    xDoc = XDocument.Load(Assembly.GetExecutingAssembly().Location.Substring(0, Assembly.GetExecutingAssembly().Location.LastIndexOf("\\")) + "\\Mapper.xml");
                    cacheManager.Add("MAPPERXML", xDoc);
                }
                else
                    xDoc = cacheManager.GetData("MAPPERXML") as XDocument;



                IList MapperContexts = (from MapperContext in xDoc.Elements("MapperContext").Elements("object").Attributes()
                                        where MapperContext.Value == id.ToString()
                                        select new
                                        {
                                            id = MapperContext.Value,
                                            name = MapperContext.NextAttribute.Value,
                                            type = MapperContext.NextAttribute.NextAttribute.Value,
                                            singletone = MapperContext.NextAttribute.NextAttribute.NextAttribute.Value

                                        }
                          ).ToList();

                if (MapperContexts != null && MapperContexts.Count == 1)
                    contextName = MapperContexts[0].ToString().Split(',')[2].Split('=')[1].Trim() +
                                    "." + MapperContexts[0].ToString().Split(',')[1].Split('=')[1].Trim() +
                                    "," + MapperContexts[0].ToString().Split(',')[2].Split('=')[1].Trim();
            }
            catch (Exception ex)
            {
            }
            

            return contextName;
        }

        
    }
}
